package com.android.pingpong;

import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback
{
    /**
     */
    private SurfaceHolder mSurfaceHolder;
    
    /**
     */
    private GameManager mGameManager;
    
    /**
     */
    public GameView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        
        mSurfaceHolder = getHolder();
        mSurfaceHolder.addCallback(this);
        
        mGameManager = new GameManager(mSurfaceHolder, context);
    }

    @Override
    /**
     * 
     */
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {
        mGameManager.initPositions(height, width);
    }

    @Override
    /**
     * 
     */
    public void surfaceCreated(SurfaceHolder holder)
    {
        mGameManager.setRunning(true);
        mGameManager.start();
    }

    @Override
 
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        boolean retry = true;
        mGameManager.setRunning(false);
        while (retry) 
        {
            try 
            {
               
                mGameManager.join(); 
                retry = false;
            } 
            catch (InterruptedException e) { }
        }
    }
}
